from datetime import datetime

from django.db import models
from django.utils.safestring import mark_safe 
from project.models import AddProject,ProjectList,UserProfile
from customer.models import Company
from django.db.models import Q

class ContractType(models.Model):
    # type_choice=(
    #     ('勘察合同','勘察合同'),
    #     ('测量合同','测量合同'),
    #     ('岩土合同','测量合同'),
    #     ('物探合同','物探合同'),
    #     ('勘测综合合同','勘测综合合同'),
    #     ('勘测设计合同','勘测设计合同'),
    #     ('触探检测协议','触探检测协议'),
    #     ('联合体合同','联合体合同'),
    #     ('技术外委合同','技术外委合同'),
    #     ('劳务外委合同','劳务外委合同'),
    #     ('设备购销采购','设备购销采购'),

    # )
    name = models.CharField(max_length=50, verbose_name='合同类型')
    class Meta:
        verbose_name = '合同类型'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name

class ContractList(models.Model):
    pay_type_choice=(
        ('总额','总额'),
        ('费率','费率'),
        ('单价','单价'),
        ('其它','其它')
    )

    '''
    保留：
    合同名称
    所属立项
    签约单位
    工程概况
    合同付款方式
    合同计费方式
    单价费率总额
    收支类型（收入、支出）
    是否结清
    确认结清人
    '''
    type = models.ForeignKey(ContractType,verbose_name='合同类型',null=True, blank=True,on_delete=models.DO_NOTHING)
    name = models.CharField(max_length=200,verbose_name='合同名称')
    addproject = models.ForeignKey(AddProject,verbose_name='所属项目',related_name='contract2project',null=True, blank=True,on_delete=models.DO_NOTHING)
    company = models.ForeignKey(Company,verbose_name='签约单位',null=True, blank=True,on_delete=models.DO_NOTHING)
    address = models.CharField(max_length=255,verbose_name='工程地址',null=True, blank=True)
    project_class = models.TextField(null=True,blank=True,verbose_name='工程规模概况')
    pay_node = models.CharField(max_length=255,verbose_name='合同约定付款方式',null=True, blank=True)
    pay_type = models.CharField(max_length=20,choices=pay_type_choice,default='总额',verbose_name='合同计费方式')
    pay_num = models.CharField(max_length=100,verbose_name='单价/总额/费率')#不限制为数字格式
    in_or_out = models.CharField(max_length=10,choices=(('收入','收入'),('支出','支出')),default='收入',verbose_name='收支类型')
    pay_total = models.DecimalField(max_digits=11, decimal_places=2,verbose_name='已收支总额（元')#收为正，支为负
    isdone = models.BooleanField(verbose_name='是否结清',default=False)
    isback = models.BooleanField(verbose_name='是否取回合同',default=False)
    handler = models.ForeignKey('users.UserProfile',verbose_name='经手人',related_name='to_handler',null=True, blank=True,on_delete=models.DO_NOTHING)
    sign_time = models.DateTimeField('合同签订时间',null=True, blank=True,default=datetime.now)
    back_time = models.DateTimeField('合同取回时间',null=True, blank=True,default=datetime.now)
    copies = models.CharField(max_length=20,verbose_name='取回合同份数',null=True, blank=True)
    by_year = models.CharField(max_length=20,verbose_name='归档年度-编号',null=True, blank=True)

    desc = models.TextField(blank=True,verbose_name='合同备注')
    def project_col(self):#增加项目列表列
        from project.models import AddProject,ProjectList
        '''
            立项名称
            |-实施项目名称
        '''
        # sub_col_name_list = AddProject.objects.filter(tempname=self.addproject.tempname)
        html = "<b><a href='/xadmin/project/addproject/?_p_id__in=%s'>%s</a></b><br>"%(self.addproject.pk,self.addproject.tempname)
        return  mark_safe(html)
    project_col.short_description = '所属项目'

    class Meta:
        verbose_name = '合同清单'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name

class PayNode(models.Model):
    contract = models.ForeignKey(ContractList,null=True, blank=True,verbose_name='合同名称',on_delete=models.DO_NOTHING)
    in_or_out = models.CharField(max_length=10,choices=(('收入','收入'),('支出','支出')),default='收入',verbose_name='收支类型')
    pay_total = models.IntegerField(default=0,verbose_name='发生总额（元）') 
    pay_time = models.DateTimeField('添加时间',default=datetime.now,blank=True)
    handler = models.ForeignKey('users.UserProfile',verbose_name='提交者',null=True, blank=True,on_delete=models.DO_NOTHING)
    desc = models.TextField(blank=True,verbose_name='备注')
    add_time = models.DateTimeField('添加时间',auto_now_add=True)
    update_time = models.DateTimeField('最后修改时间',auto_now=True)
    #每增加或一笔，contractlist同步更新金额
    class Meta:
        verbose_name = '合同支付记录'
        verbose_name_plural = verbose_name
        
    def __str__(self):
        return self.contract.name
    # def save_models(self):
    #     obj = self.new_obj
    #     ContractList.objects.fitler(id=obj.contract__id).update(pay_total=('pay_total')+obj.pay_total)
    #     super().save_models()
